<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Review Rating Synthesis
 * @version 	1.0
 * @author 		Nguyen Dinh Nhat <nguyendnhat@gmail.com>
 * @copyright   Copyright (c) 2012 ZPROFESS. (http://zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

require_once(_PS_MODULE_DIR_.'zgallery/lib/zprofess/ZadminTab.php');
include_once(_PS_MODULE_DIR_."zgallery/ZadminAlbum.php");
include_once(_PS_MODULE_DIR_."zgallery/ZadminPhoto.php");
include_once(_PS_MODULE_DIR_."zgallery/ZadminGReview.php");
include_once(_PS_MODULE_DIR_."zgallery/ZadminMemberLucky.php");

/**
 * File Admin tab
 * @author Nguyen Dinh Nhat <nguyendnhat@gmail.com>
 *
 */

class ZadminGallery extends ZadminTab 
{
	private $adminAlbum;
	private $adminGReview;
	private $adminPhoto;
	private $adminMemberLucky;
	
	private static $_Album = NULL;
	/**
	 * HÃ m dá»±ng má»›i má»¥c tiÃªu lÃ  quáº£n lÃº cÃ¡c tab con
	 * 
	 */
	public function __construct()
	{
		/*lÆ°u láº¡i id cua album Ä‘á»ƒ hiá»ƒn thá»‹ thÃ´ng tin vá»� nÃ³ má»—i láº§n*/
		$id_zgallery_album = abs((int)(Tools::getValue('id_zgallery_album')));
		if (!$id_zgallery_album) $id_zgallery_album = 1;
		self::$_Album = new Zalbum($id_zgallery_album);
		$this->adminAlbum = new ZadminAlbum ();
		$this->adminGReview = new ZadminGReview ();
		$this->adminPhoto = new ZadminPhoto ();
		$this->adminMemberLucky = new ZadminMemberLucky ();
		$this->table = array('zgallery_album', 'zgallery_photo','zgallery_review','zgallery_lucky_member');
		parent::__construct();
	}
	/**
	 * View Access in from
	 * @param bolean $disable
	 * @return bolean 
	 */
	public function viewAccess($disable = false)
	{
		$result = parent::viewAccess($disable);
		$this->adminAlbum->tabAccess = $this->tabAccess; 
		$this->adminGReview->tabAccess = $this->tabAccess;
		$this->adminPhoto->tabAccess = $this->tabAccess;
		$this->adminMemberLucky->tabAccess = $this->tabAccess;
		
		return $result;
	}
	/**
	 * Display Errors in from
	 */
	public function displayErrors()
	{
		$this->adminAlbum->displayErrors();
		$this->adminGReview->displayErrors();
		$this->adminPhoto->displayErrors();
		$this->adminMemberLucky->displayErrors();
		
	}
	/**
	 * Display fields in tab 
	 */		
	public function display()
	{
		global $currentIndex, $link, $cookie;
		if (isset($_GET['updatezgallery_album']) OR isset($_GET['addzgallery_album']) OR Tools::isSubmit('submitAddzgallery_album') OR Tools::isSubmit('submitAddzgallery_albumAndStay')) {
			$this->adminAlbum->displayForm ($this->token);	
			$id_zgallery_album =  (int)Tools::getValue('id_parent',0);
			if ($id_zgallery_album==0)
				$id_zgallery_album = 1;
			echo '<br /><br /><a href="index.php?tab='.Tools::getValue('tab').'&id_zgallery_album='.$id_zgallery_album.'&token='.$this->token.'"><img src="../img/admin/arrow2.gif" /> '.$this->l('Back to albums').'</a><br />';
		}
		else if (isset($_GET['updatezgallery_photo']) OR isset($_GET['addzgallery_photo']) OR Tools::isSubmit('submitAddzgallery_photo') OR Tools::isSubmit('submitAddzgallery_photoAndStay')) {
			$this->adminPhoto->displayForm ($this->token);
			$id_zgallery_album =  (int)Tools::getValue('id_zgallery_album',0);
			if ($id_zgallery_album==0)
				$id_zgallery_album = 1;
			echo '<br /><br /><a href="index.php?tab='.Tools::getValue('tab').'&id_zgallery_album='.$id_zgallery_album.'&token='.$this->token.'"><img src="../img/admin/arrow2.gif" /> '.$this->l('Back to photos').'</a><br />';
		}
		else if (isset($_GET['updatezgallery_review']) OR isset($_GET['addzgallery_review']) OR Tools::isSubmit('submitAddzgallery_review') OR Tools::isSubmit('submitAddzgallery_reviewAndStay')) {
			$this->adminGReview->displayForm ($this->token);	
			echo '<br /><br /><a href="'.$currentIndex.'&token='.$this->token.'"><img src="../img/admin/arrow2.gif" /> '.$this->l('Back to list').'</a><br />';
		}
		else
	 	{
	 		$id_zgallery_album = Tools::getValue('id_zgallery_album',1);
	 		$burd = Zalbum::getPath ($id_zgallery_album,$currentIndex,$this->token);
	 		
	 		echo '<div class="cat_bar"><span style="color: #3C8534;">'.$this->l('Current album').' :</span>&nbsp;&nbsp;&nbsp;';
	 			foreach ($burd as $key => $item) {
	 				echo $item['adminLink'];
	 				if ($key!=sizeof($burd)-1) {
	 					
	 					echo " &gt; ";
	 				}
	 			}
	 		echo '</div>';
			echo '
			<script type="text/javascript">
				var pos_select = '.(($tab = Tools::getValue('tabs')) ? $tab : '0').';
				$(document).ready(function(){
					$(\'#id_mvt_reason\').change(function(){
						updateMvtStatus($(this).val());
					});
					updateMvtStatus($(this).val());
				});
				function updateMvtStatus(id_mvt_reason)
				{
					if (id_mvt_reason == -1)
						return $(\'#mvt_sign\').hide();
					if ($(\'#id_mvt_reason option:selected\').attr(\'rel\') == -1)
						$(\'#mvt_sign\').html(\'<img src="../img/admin/arrow_down.png" /> '.$this->l('Decrease your stock').'\');
					else
						$(\'#mvt_sign\').html(\'<img src="../img/admin/arrow_up.png" /> '.$this->l('Increase your stock').'\');
					$(\'#mvt_sign\').show();
				}
			</script>
			<script src="../js/tabpane.js" type="text/javascript"></script>
			<link type="text/css" rel="stylesheet" href="../css/tabpane.css" />
			<form action="'.$currentIndex.'&token='.Tools::getValue('token').'" method="post" enctype="multipart/form-data" name="product" id="product">
			<input type="hidden" name="tabs" id="tabs" value="0" />
				<div class="tab-pane" id="tabPanePL">';
			$this->displayAlbum();
			$this->displayReview();
			$this->displayMBLucky ();
			echo '
			<script type="text/javascript">
				var toload = new Array();
				function loadTab(id) {
				if (toload[id]) {
					$.post(
						"'.dirname($currentIndex).'/ajax.php", {
							ajaxProductTab: id,
							token: \''.Tools::getValue('token').'\',
							id_category: '.(int)(Tools::getValue('id_category')).'},
						function(rep) {
							$("#step" + id).html(rep);var languages = new Array();
						}
					)
				}
			}
			</script>
		</div>';
		echo'
			<div class="clear"></div>
			<br />
		</form>';
	 	}
	}
	/**
	 * HiÃªÌ‰n thiÌ£ tab con review
	 */
	function displayReview()
	{
		global $cookie, $currentIndex, $attributeJs, $images;
		echo '
		<div class="tab-page" id="step2">
				<h4 class="tab">2. '.$this->l('Comment').'</h4>';
				echo '<h2>'.$this->l('Comment').'</h2>';
				$this->adminGReview->display($this->token);
		echo '
		</div>';

	}
	
	function displayMBLucky()
	{
		global $cookie, $currentIndex, $attributeJs, $images;
		echo '
		<div class="tab-page" id="step3">
				<h4 class="tab">3. '.$this->l('Member Lucky').'</h4>';
				echo '<h2>'.$this->l('Member Lucky').'</h2>';
				$this->adminMemberLucky->display($this->token);
				ZluckyMember::createFileCsv ();
				echo '<br/><ul style="padding-left: 1px;"><li style="display:inline;text-decoration: underline"><a download_url="../modules/zgallery/export/DanhSachThanhVienMayMan.csv" href="../modules/zgallery/export/DanhSachThanhVienMayMan.csv">Get File CSV for List Member Lucky</a></li></ul>';
		echo '
		</div>';

	}
	/**
	 * HiÃªÌ‰n thiÌ£ tab con Album
	 */
	function displayAlbum()
	{
		parent::displayForm(false);
		global $currentIndex, $cookie, $link;
		$id_zgallery_album =  (int)Tools::getValue('id_zgallery_album',0);
			if ($id_zgallery_album==0)
				$id_zgallery_album = 1;
		echo '
		<form ></form >
		<div class="tab-page" id="step1">
			
			';
			if ($id_zgallery_album==1) {
				echo '<h4 class="tab">1. '.$this->l('Albums').'</h4><h2>'.$this->l('Albums').'</h2>';
				$this->adminAlbum->display ($this->token);
			} else {
				echo '<h4 class="tab">1. '.$this->l('Photos').'</h4><h2>'.$this->l('Photos in this Album').'</h2>';
				$this->adminPhoto->display ($this->token);
			}
		echo '</div>';
	}
	/**
	 * Save Action, add, update for from 
	 */
	public function postProcess()
	{
		global $cookie;
		$this->adminGReview->token=$this->token;
		$this->adminPhoto->token=$this->token;
		$this->adminAlbum->token=$this->token;
		$this->adminMemberLucky->token=$this->token;
		
		if (Tools::getValue('id_zgallery_album') || Tools::isSubmit('submitDelzgallery_album') || Tools::isSubmit('submitFilterzgallery_album')|| Tools::isSubmit('submitAddzgallery_album') || Tools::isSubmit('submitFilter')|| Tools::isSubmit('submitReset') || Tools::isSubmit('zgallery_albumOrderway')){
			if (Tools::isSubmit('id_zgallery_album') AND !Tools::getValue('id_zgallery_album')) 
			 	$_POST['id_zgallery_album']=1;
			$this->adminAlbum->postProcess($this->token);
		}
		if (Tools::getValue('id_zgallery_photo') || Tools::isSubmit('submitDelzgallery_photo') || Tools::isSubmit('submitFilterzgallery_photo')|| Tools::isSubmit('submitAddzgallery_photo') ){
					
			$this->adminPhoto->postProcess($this->token);
		}
		if (Tools::getValue('id_zgallery_review') || Tools::isSubmit('submitDelzgallery_review') || Tools::isSubmit('submitFilterzgallery_review')|| Tools::isSubmit('submitAddzgallery_review') ){
			$this->adminGReview->postProcess($this->token);
		}
		if (Tools::getValue('id_zgallery_lucky_member') || Tools::isSubmit('submitDelzgallery_lucky_member') || Tools::isSubmit('submitFilterzgallery_lucky_member')|| Tools::isSubmit('submitAddzgallery_lucky_member') || Tools::isSubmit('deletezgallery_lucky_member') ){
			$this->adminMemberLucky->postProcess($this->token);
			$_GET['tabs']=2;
		}
	}
	
	/**
	 * Return current Album
	 *
	 * @return object
	 */
	public static function getCurrentAlbum()
	{
		return self::$_Album;
	}
}


